home *** CD-ROM | disk | FTP | other *** search
/ Windows Expert / Windows Expert.iso / sound / muzika.zip / MUZSRC1.ZIP / MAIN.CPP < prev    next >
C/C++ Source or Header  |  1992-07-21  |  6KB  |  216 lines

  1. // **********************************************
  2. // File: MAIN.CPP
  3. // THE main module
  4.  
  5. #include "muzika.h"
  6.  
  7. HANDLE hInst;
  8. HWND hMainWnd;
  9.  
  10. long FAR PASCAL MainWindowProc(HWND, unsigned, WORD, LONG);
  11.  
  12. // **********************************************
  13. // WinMain is the program entry point.
  14.  
  15. int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance, LPSTR, int cmdShow)
  16. {
  17.   MSG msg;
  18.  
  19.   // Perform first initialization
  20.   InitMain(hInstance, hPrevInstance, cmdShow);
  21.  
  22.   // Enter a message-polling loop
  23.   while (GetMessage(&msg, NULL, 0, 0)) {
  24.     TranslateMessage(&msg);
  25.     DispatchMessage(&msg);
  26.   }
  27.  
  28.   // When quit message received, close the application
  29.   CloseMain(hInstance);
  30.   return (msg.wParam);
  31. }
  32.  
  33. // **********************************************
  34. // InitMain performs an initialization of a MUZIKA application instance.
  35.  
  36. void InitMain(HANDLE hInstance, HANDLE hPrevInstance, int cmdShow)
  37. {
  38.   // Check whether first instance of the application
  39.   if (!hPrevInstance)
  40.     InitMainFirst(hInstance);
  41.   else
  42.     InitMainAdded(hInstance, hPrevInstance);
  43.  
  44.   // Do every-instance initialization
  45.   InitMainEvery(hInstance, cmdShow);
  46. }
  47.  
  48. // **********************************************
  49. // InitMainFirst performs a first-instance initialization.
  50. // It registers the main and edit window classes.
  51.  
  52. void InitMainFirst(HANDLE hInstance)
  53. {
  54.   WNDCLASS wc;
  55.  
  56.   // Register the main window class
  57.   wc.lpszClassName = "MUZIKA";
  58.   wc.hInstance = hInstance;
  59.   wc.lpfnWndProc = MainWindowProc;
  60.   wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  61.   wc.hIcon = LoadIcon(hInstance, "I_MUZIKA");
  62.   wc.lpszMenuName = "M_MUZIKA";
  63.   wc.hbrBackground = COLOR_WINDOW+1;
  64.   wc.style = CS_HREDRAW | CS_VREDRAW;
  65.   wc.cbClsExtra = 0;
  66.   wc.cbWndExtra = 0;
  67.   RegisterClass(&wc);
  68.  
  69.   RegisterEditClass(hInstance);
  70. }
  71.  
  72. // **********************************************
  73. // InitMainAdded performs an initialization of any instance of MUZIKA
  74. // other than the first. No class registration is necessary.
  75.  
  76. void InitMainAdded(HANDLE, HANDLE)
  77. {
  78. }
  79.  
  80. // **********************************************
  81. // InitMainEvery does an every-instance initialization. It displays
  82. // the main and edit windows, and initializes the main menu.
  83.  
  84. void InitMainEvery(HANDLE hInstance, int cmdShow)
  85. {
  86.   hInst = hInstance;
  87.  
  88.   // Create and display the main window
  89.   hMainWnd = CreateWindow("MUZIKA",
  90.     "MUZIKA (Untitled)",
  91.     WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
  92.     0, 0,
  93.     CW_USEDEFAULT, 0,
  94.     NULL,
  95.     NULL,
  96.     hInstance,
  97.     NULL);
  98.   ShowWindow(hMainWnd, cmdShow);
  99.   UpdateWindow(hMainWnd);
  100.  
  101.   hMainMenu = GetMenu(hMainWnd);
  102.   InitializeMenu(FALSE);
  103.  
  104.   CreateEditWindow(hInstance);
  105. }
  106.  
  107. // **********************************************
  108. // CloseMain destroys the melody database when the application is closed.
  109.  
  110. void CloseMain(HANDLE)
  111. {
  112.   for (int index = 0; index < melody.part.number(); ++index)
  113.     melody.part.destroyAt(index);
  114. }
  115.  
  116. // **********************************************
  117. // MainWindowProc is the main window function, which responds to messages
  118. // intended for the main window. The messages processed are described within.
  119.  
  120. long FAR PASCAL MainWindowProc(
  121.   HWND hWnd, unsigned message, WORD wParam, LONG lParam)
  122. {
  123.   // Check message type
  124.   switch(message) {
  125.     case WM_PAINT:
  126.       // Process a WM_PAINT message, indicating that the window
  127.       // should be repainted.
  128.       PaintMainWindow(hWnd);
  129.       break;
  130.  
  131.     case WM_CLOSE:
  132.       // Process a WM_CLOSE message, indicating that the user has
  133.       // requested to close the window. If there is an unsaved melody,
  134.       // it should be verified that the user indeed means to exit.
  135.       AskSave();
  136.       DestroyWindow(hWnd);
  137.       break;
  138.  
  139.     case WM_DESTROY:
  140.       // Process a WM_DESTROY message, indicating that the main window
  141.       // has been closed. A quit message is to be sent to the application
  142.       // in order to stop the main loop in WinMain.
  143.       PostQuitMessage(0);
  144.       break;
  145.  
  146.     case WM_LBUTTONDOWN:
  147.       // Process a WM_LBUTTONDOWN message, indicating that the user has clicked
  148.       // the left mouse button. The symbol on which the cursor is clicked
  149.       // should be made current.
  150.       if (IdentifyEditModeSymbol(lParam) || IdentifySymbol(lParam));
  151.       break;
  152.  
  153.     case WM_SIZE:
  154.       // Process a WM_SIZE message, indicating that the main window size
  155.       // has changed. The edit window size should be adjusted appropriately.
  156.       MoveWindow(hEditWnd, 73, 37, LOWORD(lParam)-72, HIWORD(lParam)-36, TRUE);
  157.       break;
  158.  
  159.     case WM_COMMAND:
  160.       // Process a WM_COMMAND message, indicating that the user has made
  161.       // a menu selection. The selection is processed further in ProcessMenu.
  162.       if (!ProcessMenu(wParam))
  163.         return DefWindowProc(hWnd, message, wParam, lParam);
  164.       break;
  165.  
  166.     default:
  167.       // Unrecognized message: just let Windows take care of it.
  168.       return DefWindowProc(hWnd, message, wParam, lParam);
  169.   }
  170.  
  171.   return 0L;
  172. }
  173.  
  174. // **********************************************
  175. // PaintMainWindow is the main window painting function,
  176. // activated whenever the main window receives a WM_PAINT message.
  177. // It redraws the main window, including lines and symbols.
  178.  
  179. void PaintMainWindow(HWND hWnd)
  180. {
  181.   PAINTSTRUCT ps;
  182.   HDC hDC;
  183.   int LineIndex;
  184.   RECT rect;
  185.  
  186.   // Obtain a display context
  187.   hDC = BeginPaint(hWnd, &ps);
  188.  
  189.   // Set the mapping mode parameters
  190.   SetMapMode(hDC, MM_TEXT);
  191.   GetClientRect(hWnd, &rect);
  192.  
  193.   // Draw the layout of the lines
  194.   MoveTo(hDC, 0, 36);
  195.   LineTo(hDC, rect.right, 36);
  196.   MoveTo(hDC, 0, 0);
  197.   LineTo(hDC, 0, rect.bottom);
  198.   MoveTo(hDC, 36, 0);
  199.   LineTo(hDC, 36, rect.bottom);
  200.   MoveTo(hDC, 72, 0);
  201.   LineTo(hDC, 72, rect.bottom);
  202.   MoveTo(hDC, 108, 0);
  203.   LineTo(hDC, 108, 36);
  204.   for (LineIndex = 60; LineIndex < rect.bottom; LineIndex += 24) {
  205.     MoveTo(hDC, 0, LineIndex);
  206.     LineTo(hDC, 72, LineIndex);
  207.   }
  208.  
  209.   // Draw the symbols
  210.   DisplayEditModeSymbols(hDC);
  211.   RefreshSymbols(hDC);
  212.  
  213.   // Finish the window updating
  214.   EndPaint(hWnd, &ps);
  215. }
  216.